package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.VtuzxException;
import com.vtuzx.core.bean.dto.VtuzxFile;
import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcTestRoomService;
import com.vtuzx.kingdee.xqc_api.service.IXqcTraceBackService;
import com.vtuzx.web.constant.ApiType;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 检验室
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/test_room")
@RestController
public class XqcTestRoomController {

    @Autowired
    IXqcTestRoomService testRoomService;

    /**
     * 获取检验室列表
     */
    @PostMapping("/get_test_room_list")
    @VtuzxApi
    public Object getTestRoomList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize, 10);
        return testRoomService.getTestRoomList(_userId, param, pageIndex, pageSize);
    }

    /**
     * 检验合格
     */
    @PostMapping("/test_qualified")
    @VtuzxApi
    public Object testQualified(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        String testMode = VtuzxUtil.getString(param, "testMode");
        testRoomService.testQualified(_userId, name, billNo, testMode);
        return null;
    }


    /**
     * 上传报告
     */
    @PostMapping("/upload_report")
    @VtuzxApi
    public Object uploadReport(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("files") MultipartFile[] files) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String billNo = VtuzxUtil.getString(param, "billNo");
        List<VtuzxFile> vFileList = new ArrayList<>();
        if (files.length != 0) {
            vFileList = VtuzxUtil.getObject(param, "files");
        }
        String picStr = VtuzxUtil.getString(param, "picList");
        testRoomService.uploadReport(_userId, name, billNo, vFileList, picStr);
        return null;
    }



    /**
     * 新增生产过程
     */
    @PostMapping("/add_prd_process")
    @VtuzxApi
    public Object addPrdProcess(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        testRoomService.addPrdProcess(_userId, name, param);
        return null;
    }

    /**
     * 更新生产过程
     */
    @PostMapping("/update_prd_process")
    @VtuzxApi
    public Object updatePrdProcess(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("files") MultipartFile[] files) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = VtuzxUtil.getObject(param, "files");
        testRoomService.updatePrdProcess(_userId, name, param, vFileList);
        return null;
    }

    /**
     * 删除生产过程
     */
    @PostMapping("/delete_prd_process")
    @VtuzxApi
    public Object deletePrdProcess(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        testRoomService.deletePrdProcess(_userId, name, param);
        return null;
    }

    /**
     * 获取生产过程列表
     */
    @PostMapping("/get_prd_process_list")
    @VtuzxApi
    public Object getPrdProcessList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        int pageIndex = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageIndex);
        int pageSize = VtuzxUtil.getInt(param, IVtuzxConst.Key_pageSize);
        return testRoomService.getPrdProcessList(_userId, name, param, pageIndex, pageSize);
    }

    /**
     * 按条件获取生产过程列表
     */
    @PostMapping("/get_prd_process_list_key")
    @VtuzxApi
    public Object getPrdProcessListKey(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        String key = VtuzxUtil.getString(param, "key");
        return testRoomService.getPrdProcessListKey(_userId, name, key);
    }

    /**
     * 添加证书
     */
    @PostMapping("/add_certificate")
    @VtuzxApi
    public Object addCertificate(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("files") MultipartFile[] files) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = new ArrayList<>();
        if (files.length != 0) {
            vFileList = VtuzxUtil.getObject(param, "files");
        }
        testRoomService.addCertificate(_userId, name, param, vFileList);
        return null;
    }

    /**
     * 添加视频
     */
    @PostMapping("/add_video")
    @VtuzxApi
    public Object addVideo(@VtuzxControlParam VtuzxToken token, @RequestParam Map<String, Object> param, @RequestParam("files") MultipartFile[] files) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        List<VtuzxFile> vFileList = new ArrayList<>();
        if (files.length != 0) {
            vFileList = VtuzxUtil.getObject(param, "files");
        }
//        testRoomService.addVideo(_userId, name, param, vFileList);
        return null;
    }

    /**
     * 查询证书列表
     */
    @PostMapping("/get_certificate_list")
    @VtuzxApi
    public Object getCertificateList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return testRoomService.getCertificateList(_userId, name, param);
    }

    /**
     * 删除证书图片
     */
    @PostMapping("/delete_certificate_img")
    @VtuzxApi
    public Object deleteCertificateImg(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        testRoomService.deleteCertificateImg(_userId, name, param);
        return null;
    }

    /**
     * 删除证书
     */
    @PostMapping("/delete_certificate")
    @VtuzxApi
    public Object deleteCertificate(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        testRoomService.deleteCertificate(_userId, name, param);
        return null;
    }

    /**
     * 删除证书
     */
    @PostMapping("/download_test_room_list")
    @VtuzxApi(type = ApiType.Download)
    public Object downloadTestRoomList(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) throws Exception {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String name = VtuzxUtil.getString(token, "name");
        return testRoomService.downloadTestRoomList(_userId, name, param);
    }

}
